package gp;


import java.util.List;

import firing.FiringTree;


public class TournamentSelection implements SelectionStrategy, IndexSelectionStrategy {
    public static final int TOURNAMENT_SIZE = 5;

    /** Randomly selects a tournament of individuals, returns the index of the fittest one. Assumes population is sorted from least to most fit */
    public int getIndex(int populationSize) {
	int index = -1;
	for(int i=0;i<TOURNAMENT_SIZE;i++) {
	    int challenger = (int)(Math.random()*populationSize);
	    if (challenger > index) {
		index = challenger;
	    }
	}
	return index;
    }
    
    public List<FiringTree> getNextGen(List<FiringTree> prevGen) {
	return Generationator.getNextGen(prevGen, this);
    }
    
    public String toString() {
	return "Tournament";
    }
}